package com.tarena.lbs.marketing.web.controller;

import com.tarena.lbs.marketing.web.service.CouponService;
import com.tarena.lbs.marketing.web.service.UserReceiveCouponService;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.marketing.converter.CouponConverter;
import com.tarena.lbs.common.marketing.converter.UserReceiveCouponConverter;
import com.tarena.lbs.pojo.marketing.bo.CouponBO;
import com.tarena.lbs.pojo.marketing.bo.UserReceiveCouponBO;
import com.tarena.lbs.pojo.marketing.param.CouponParam;
import com.tarena.lbs.pojo.marketing.param.UpdateParam;
import com.tarena.lbs.pojo.marketing.query.CouponQuery;
import com.tarena.lbs.pojo.marketing.query.UserCouponQuery;
import com.tarena.lbs.pojo.marketing.vo.CouponVO;
import com.tarena.lbs.pojo.marketing.vo.UserReceiveCouponVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/admin/marketing/coupon/info")
@Api(tags = "优惠券管理")
public class CouponController {

    @Resource
    private CouponService couponService;

    @Resource
    private UserReceiveCouponService userReceiveCouponService;

    @Resource
    private CouponConverter couponConverter;

    @Resource
    private UserReceiveCouponConverter userReceiveCouponConverter;

    @PostMapping("/save")
    @ApiOperation("新增优惠券")
    public Result<Void> insert(@Validated @RequestBody CouponParam couponParam) throws BusinessException {
        couponService.insert(couponParam);
        return Result.success();
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("优惠券详情")
    public Result<CouponVO> couponDetail(@PathVariable("id") Integer couponId) {
        CouponBO couponBO = couponService.getDetail(couponId);
        return new Result<>(couponConverter.bo2vo(couponBO));
    }

    @PostMapping("/list")
    @ApiOperation("查询优惠券列表")
    public Result<PageResult<CouponVO>> query(@Valid @RequestBody CouponQuery couponQuery) {
        BasePageList<CouponBO> couponBos = couponService.list(couponQuery);
        PageResult<CouponVO> page = couponConverter.assemblePageVOS(couponBos,couponQuery);
        return new Result<>(page);
    }

    @PostMapping("/update")
    @ApiOperation("更新优惠券")
    public Result<Void> update(@Valid @RequestBody CouponParam couponParam) throws BusinessException {
        couponService.insert(couponParam);
        return Result.success();
    }

    @PostMapping("/updateStatus")
    @ApiOperation("更新优惠券状态")
    public Result<Void> updateCouponStatus(@Valid @RequestBody UpdateParam updateParam) {
        couponService.updateStatus(updateParam);
        return Result.success();
    }

    @PostMapping("/queryReceive")
    @ApiOperation("查询领取记录列表")
    public Result<PageResult<UserReceiveCouponVO>> query(@Valid @RequestBody UserCouponQuery query){
        BasePageList<UserReceiveCouponBO> couponBos = userReceiveCouponService.userList(query);
        PageResult<UserReceiveCouponVO> page = userReceiveCouponConverter.assemblePageVOS(couponBos,query);
        return new Result<>(page);
    }

}
